home *** CD-ROM | disk | FTP | other *** search
/ Dynamic HTML Construction Kit / Dynamic HTML Construction Kit.iso / earthlink / nscomm / java40.jar / java / net / Socket.class (.txt) < prev    next >
Encoding:
Java Class File  |  1997-11-03  |  3.8 KB  |  165 lines

  1. package java.net;
  2.  
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.io.OutputStream;
  6.  
  7. public class Socket {
  8.    SocketImpl impl;
  9.    private static SocketImplFactory factory;
  10.  
  11.    protected Socket() {
  12.       this.initSocketImpl();
  13.    }
  14.  
  15.    protected void initSocketImpl() {
  16.       this.impl = this.createSocketImpl();
  17.    }
  18.  
  19.    protected SocketImpl createSocketImpl() {
  20.       return (SocketImpl)(factory != null ? factory.createSocketImpl() : new PlainSocketImpl());
  21.    }
  22.  
  23.    protected Socket(SocketImpl var1) throws SocketException {
  24.       this.impl = var1;
  25.    }
  26.  
  27.    public Socket(String var1, int var2) throws UnknownHostException, IOException {
  28.       this(InetAddress.getByName(var1), var2, (InetAddress)null, 0, true);
  29.    }
  30.  
  31.    public Socket(InetAddress var1, int var2) throws IOException {
  32.       this(var1, var2, (InetAddress)null, 0, true);
  33.    }
  34.  
  35.    public Socket(String var1, int var2, InetAddress var3, int var4) throws IOException {
  36.       this(InetAddress.getByName(var1), var2, var3, var4, true);
  37.    }
  38.  
  39.    public Socket(InetAddress var1, int var2, InetAddress var3, int var4) throws IOException {
  40.       this(var1, var2, var3, var4, true);
  41.    }
  42.  
  43.    public Socket(String var1, int var2, boolean var3) throws IOException {
  44.       this(InetAddress.getByName(var1), var2, (InetAddress)null, 0, var3);
  45.    }
  46.  
  47.    public Socket(InetAddress var1, int var2, boolean var3) throws IOException {
  48.       this(var1, var2, (InetAddress)null, 0, var3);
  49.    }
  50.  
  51.    private Socket(InetAddress var1, int var2, InetAddress var3, int var4, boolean var5) throws IOException {
  52.       this.initSocketImpl();
  53.       if (var2 >= 0 && var2 <= 65535) {
  54.          if (var4 >= 0 && var4 <= 65535) {
  55.             System.getSecurityManager().checkConnect(var1.getHostAddress(), var2);
  56.  
  57.             try {
  58.                this.impl.create(var5);
  59.                if (var3 != null || var4 > 0) {
  60.                   if (var3 == null) {
  61.                      var3 = InetAddress.anyLocalAddress;
  62.                   }
  63.  
  64.                   this.impl.bind(var3, var4);
  65.                }
  66.  
  67.                this.impl.connect(var1, var2);
  68.             } catch (SocketException var7) {
  69.                this.impl.close();
  70.                throw var7;
  71.             }
  72.          } else {
  73.             throw new IllegalArgumentException("port out range:" + var4);
  74.          }
  75.       } else {
  76.          throw new IllegalArgumentException("port out range:" + var2);
  77.       }
  78.    }
  79.  
  80.    public InetAddress getInetAddress() {
  81.       return this.impl.getInetAddress();
  82.    }
  83.  
  84.    public InetAddress getLocalAddress() {
  85.       Object var1 = null;
  86.  
  87.       try {
  88.          var3 = (InetAddress)this.impl.getOption(15);
  89.       } catch (Exception var2) {
  90.          var3 = InetAddress.anyLocalAddress;
  91.       }
  92.  
  93.       return var3;
  94.    }
  95.  
  96.    public int getPort() {
  97.       return this.impl.getPort();
  98.    }
  99.  
  100.    public int getLocalPort() {
  101.       return this.impl.getLocalPort();
  102.    }
  103.  
  104.    public InputStream getInputStream() throws IOException {
  105.       SecurityManager.enablePrivilege("UniversalConnect");
  106.       return this.impl.getInputStream();
  107.    }
  108.  
  109.    public OutputStream getOutputStream() throws IOException {
  110.       SecurityManager.enablePrivilege("UniversalConnect");
  111.       return this.impl.getOutputStream();
  112.    }
  113.  
  114.    public void setTcpNoDelay(boolean var1) throws SocketException {
  115.       this.impl.setOption(1, new Boolean(var1));
  116.    }
  117.  
  118.    public boolean getTcpNoDelay() throws SocketException {
  119.       return (Boolean)this.impl.getOption(1);
  120.    }
  121.  
  122.    public void setSoLinger(boolean var1, int var2) throws SocketException {
  123.       if (!var1) {
  124.          this.impl.setOption(128, new Boolean(var1));
  125.       } else {
  126.          this.impl.setOption(128, new Integer(var2));
  127.       }
  128.    }
  129.  
  130.    public int getSoLinger() throws SocketException {
  131.       Object var1 = this.impl.getOption(128);
  132.       return var1 instanceof Integer ? (Integer)var1 : -1;
  133.    }
  134.  
  135.    public synchronized void setSoTimeout(int var1) throws SocketException {
  136.       this.impl.setOption(4102, new Integer(var1));
  137.    }
  138.  
  139.    public synchronized int getSoTimeout() throws SocketException {
  140.       Object var1 = this.impl.getOption(4102);
  141.       return var1 instanceof Integer ? (Integer)var1 : 0;
  142.    }
  143.  
  144.    public synchronized void close() throws IOException {
  145.       this.impl.close();
  146.    }
  147.  
  148.    public String toString() {
  149.       return "Socket[addr=" + this.impl.getInetAddress() + ",port=" + this.impl.getPort() + ",localport=" + this.impl.getLocalPort() + "]";
  150.    }
  151.  
  152.    public static synchronized void setSocketImplFactory(SocketImplFactory var0) throws IOException {
  153.       if (factory != null) {
  154.          throw new SocketException("factory already defined");
  155.       } else {
  156.          SecurityManager var1 = System.getSecurityManager();
  157.          if (var1 != null) {
  158.             var1.checkSetFactory();
  159.          }
  160.  
  161.          factory = var0;
  162.       }
  163.    }
  164. }
  165.